//
//  FDRegisterController.m
//  FamousDoctorOnline
//
//  Created by wszyxc on 16/7/6.
//  Copyright © 2016年 wszyxc. All rights reserved.
//

#import "FDRegisterController.h"
#import <SVProgressHUD.h>
#import "FDPersonController.h"



@interface FDRegisterController ()

@property (weak,nonatomic) UITextField * numField;

@property (weak,nonatomic) UITextField * securityField;

@property (weak,nonatomic) UIButton * getSecurityBtn;

@property (weak,nonatomic) UILabel * textLabel;

@property (weak,nonatomic) UILabel * protocalLabel;

@property (weak,nonatomic) UIButton * registerBtn;

@property (weak,nonatomic) UIButton * agreeBtn;

@property (weak,nonatomic) NSTimer * timer;


@end


NSInteger timeCount = 10;
NSInteger securityNum;


@implementation FDRegisterController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    [self setupLayout];

}

- (void)setupUI{
    
    //添加nameField
    
    UITextField *numField = [[UITextField alloc]init];
    numField.placeholder = @"请输入手机号";
    numField.background = [UIImage imageNamed:@"shurukuang"];
    
    numField.keyboardType = UIKeyboardTypeNumberPad;
    
    numField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 10)];
    
    numField.leftViewMode = UITextFieldViewModeAlways;
    
    [self.view addSubview:numField];
    
    self.numField = numField;
    
    [numField addTarget:self action:@selector(fieldChanged) forControlEvents:UIControlEventEditingChanged];
    
    //添加passwordField
    
    UITextField *securityField = [[UITextField alloc]init];
    
    securityField.placeholder = @"请输入验证码";
    
    securityField.background = [UIImage imageNamed:@"shurukuang"];
    
    securityField.keyboardType = UIKeyboardTypeNumberPad;
    
    securityField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 10)];
    
    securityField.leftViewMode = UITextFieldViewModeAlways;
    
    [self.view addSubview:securityField];
    
    self.securityField = securityField;
    
    [self.securityField addTarget:self action:@selector(fieldChanged) forControlEvents:UIControlEventEditingChanged];
    
    //添加获取验证码的button
    UIButton *getSecurityBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    self.getSecurityBtn = getSecurityBtn;
    
    [getSecurityBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    
    [getSecurityBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
    
    [getSecurityBtn setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
    
    [getSecurityBtn setBackgroundColor:[UIColor grayColor]];
    
    [self.view addSubview:getSecurityBtn];
    
    [getSecurityBtn addTarget:self action:@selector(getSecurityClick) forControlEvents:UIControlEventTouchUpInside];
    
    getSecurityBtn.enabled = NO;
    
    //添加label
    UILabel * textLabel = [self setupOneLabelWithTitle:@"本人同意并阅读" testColor:[UIColor blackColor] fontSize:15];
    
    self.textLabel = textLabel;
    
    [self.view addSubview:textLabel];
    
    //添加lebel
    UILabel * protocalLabel = [self setupOneLabelWithTitle:@"用户协议" testColor:BLUE_COLOR fontSize:15];
    
    self.protocalLabel = protocalLabel;
    
    [self.view addSubview:protocalLabel];
    
    //添加注册按钮
    
    UIButton *registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    self.registerBtn = registerBtn;
    
    [registerBtn setTitle:@"注册" forState:UIControlStateNormal];
    
    [registerBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
    
    [registerBtn setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
    
    [registerBtn setBackgroundImage:[UIImage imageNamed:@"nav"] forState:UIControlStateNormal];
    
    [self.view addSubview:registerBtn];
    
    [registerBtn addTarget:self action:@selector(registerClick) forControlEvents:UIControlEventTouchUpInside];
    
    registerBtn.enabled = NO;
    
    //添加是否同意按钮
//    [self.SwithchBtn2 setImage:[UIImage imageNamed:@"illness_rb_img_nor.png"] forState:UIControlStateNormal];
//    [self.SwithchBtn2 setImage:[UIImage imageNamed:@"illness_rb_img_sel.png"] forState:UIControlStateSelected];
    
    UIButton *agreeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    self.agreeBtn = agreeBtn;
    
    [agreeBtn setImage:[UIImage imageNamed:@"illness_rb_img_nor.png"] forState:UIControlStateNormal];
    
    [agreeBtn setImage:[UIImage imageNamed:@"illness_rb_img_sel.png"] forState:UIControlStateSelected];
    
    [self.view addSubview:agreeBtn];
    
    [agreeBtn addTarget:self action:@selector(agreeClick) forControlEvents:UIControlEventTouchUpInside];
    
    self.agreeBtn.enabled = NO;
   
}


//点击同意
- (void)agreeClick{
    
    self.agreeBtn.selected = !self.agreeBtn.isSelected;
    
    self.registerBtn.enabled = self.agreeBtn.selected;
   
}

//field值编辑改变
- (void)fieldChanged{
    
    NSInteger rightCount=0;
    
    NSLog(@"值改变");
    NSString *phoneNum = self.numField.text;
    
    //判断是否是手机号码
    if([self valiMobile:phoneNum]){
        
        self.numField.textColor = BLUE_COLOR;
        rightCount++;
        
        self.getSecurityBtn.enabled = YES;
        NSLog(@"改为蓝色");
        
    }else{
        
        self.numField.textColor = [UIColor blackColor];
    }


    
    if ([self.securityField.text isEqualToString:[NSString stringWithFormat:@"%ld",securityNum]]) {
        
        NSLog(@"验证码输入正确");
        if (securityNum!=0) {
            
            self.securityField.textColor = BLUE_COLOR;
            rightCount++;
        }else{
            
            [SVProgressHUD showInfoWithStatus:@"请先获取验证码"];
            
            [self performSelector:@selector(dismissSV) withObject:nil afterDelay:1];
            
            
        }
    }else{
        
        self.securityField.textColor = [UIColor blackColor];
    }
    
   
    if (rightCount==2) {
        
        NSLog(@"都正确");
        
        self.agreeBtn.enabled = YES;
        
    }
    
    
}

/**
 *  判断一个号码是否是合法的手机号码
 *

 */
- (BOOL)valiMobile:(NSString *)mobile{

    
    if (mobile.length<11) {
        return NO;
    }else if (mobile.length>11){
        
        self.numField.text = [self.numField.text substringToIndex:11];
        
        mobile = self.numField.text;
        
        [SVProgressHUD showInfoWithStatus:@"您输入的手机号码长度已超出!"];
        
        [self performSelector:@selector(dismissSV) withObject:nil afterDelay:1];
    }
    
    if (mobile.length==11)
    {
        /**
         * 移动号段正则表达式
         */
        NSString *CM_NUM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
        /**
         * 联通号段正则表达式
         */
        NSString *CU_NUM = @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
        /**
         * 电信号段正则表达式
         */
        NSString *CT_NUM = @"^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$";
        NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM_NUM];
        BOOL isMatch1 = [pred1 evaluateWithObject:mobile];
        NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU_NUM];
        BOOL isMatch2 = [pred2 evaluateWithObject:mobile];
        NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT_NUM];
        BOOL isMatch3 = [pred3 evaluateWithObject:mobile];
        
        if (isMatch1 || isMatch2 || isMatch3) {
            return YES;
        }else{
            return NO;
        }
    }
    else{
        
        
        
        
        
    }
    return NO;
}


/**
 *  点击了获取验证码
 */

- (void)getSecurityClick{
    
    NSLog(@"获取验证码");
    
    self.getSecurityBtn.enabled = NO;
    
    [self.getSecurityBtn setTitle:@"10" forState:UIControlStateNormal];
    
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(disTimeDesc) userInfo:nil repeats:YES];
    
    NSLog(@"%@",timer);
    
    self.timer  = timer;
}

/**
 *  时间倒计时显示处理
 */

- (void)disTimeDesc{
    
    if (timeCount-- > 1) {
        
        [self.getSecurityBtn setTitle:[NSString stringWithFormat:@"%ld",(long)timeCount] forState:UIControlStateNormal];
    }else{
        
        [self.getSecurityBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [self.timer invalidate];
        
        self.timer = nil;
        
        self.getSecurityBtn.enabled = YES;
        
        timeCount = 10;
        
        securityNum = arc4random_uniform(9000)+1000;
        
        [SVProgressHUD showInfoWithStatus:[NSString stringWithFormat:@"您的验证码是%04ld",securityNum]];
       
        
    }
    
    
    
}



/**
 *  点击注册
 */

- (void)registerClick{
    
    NSLog(@"点击了注册,跳转界面");
    
    //能点击就说明输入都正确,那就跳转界面
    
    //先取出已存储的用户id的最大值
    
    FDPersonController *personVC = [[FDPersonController alloc]init];
    
    [self.navigationController pushViewController:personVC animated:YES];
    

   
}

- (void)dismissSV{
    
    [SVProgressHUD dismiss];
    
}


- (void)setupLayout{
    
    
    [self.numField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.offset(100);
        make.leading.offset(10);
        make.trailing.offset(-10);
        make.height.offset(30);
        
        
    }];
    
    [self.securityField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.offset(10);
        make.top.equalTo(self.numField.mas_bottom).offset(20);
        make.height.offset(30);
        
    }];
    
    [self.getSecurityBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.equalTo(self.securityField.mas_centerY);
        
        make.trailing.offset(-10);
        
        make.leading.equalTo(self.securityField.mas_trailing).offset(10);
        
        make.width.offset(100);
      
    }];
    
    [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.securityField.mas_bottom).offset(40);
        make.leading.offset(100);
       
    }];
    
    [self.protocalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.textLabel.mas_centerY);
        make.leading.equalTo(self.textLabel.mas_trailing).offset(5);
    
    }];
    
    [self.registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.offset(10);
        make.trailing.offset(-10);
        make.top.equalTo(self.textLabel.mas_bottom).offset(35);
        make.height.offset(45);
        
    }];
    
    [self.agreeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        
        make.centerY.equalTo(self.textLabel.mas_centerY);
        
        make.trailing.equalTo(self.textLabel.mas_leading).offset(-10);
        
        make.height.offset(30);
        
    }];
    
    
}


/**
 *  添加一个label的快捷方法
 */
- (UILabel *)setupOneLabelWithTitle:(NSString *)title testColor:(UIColor *)color fontSize:(CGFloat)fontSize{
    
    UILabel *label = [[UILabel alloc]init];
    
    label.textColor = color;
    
    label.font = [UIFont systemFontOfSize:fontSize];
    
    label.text = title;
    
    return label;
    
}

//键盘回退
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    
    [self.view endEditing:YES];
}


@end
